#include "send_msg.h"

Send_Msg::Send_Msg(Http_Post *parent) :
	Http_Post(parent)
{

}

Send_Msg::~Send_Msg()
{

}

void Send_Msg::m_saveLog(QString fromUserName, QString toUserName, QString content)
{
	QFile file(QWXDIR + "/" + toUserName + ".txt");
	if (file.open(QIODevice::Append | QIODevice::Text)) {
		QTextStream out(&file);
		out << QString::number(time(NULL)) << DELIM << fromUserName << DELIM
			<< content << "\n";
		file.close();
	}
}

void Send_Msg::sendV2(QString uin, QString sid, QString skey, QString deviceId,
					 QString fromUserName, QString toUserName, QString content, QStringList syncKey)
{
	m_saveLog(fromUserName, toUserName, content);

	postV2(uin, sid, skey, deviceId, fromUserName, toUserName, content);
	//syncV2(uin, sid, skey, syncKey);
}

void Send_Msg::m_post(QString host, QString uin, QString sid, QString skey,
					 QString deviceId, QString fromUserName, QString toUserName, QString content)
{
	QString ts = QString::number(time(NULL));
	QString url = host + WX_CGI_PATH + "webwxsendmsg?sid=" + sid
		+ "&skey=" + skey + "&r=" + ts;
#if 0//QWX_DEBUG
	qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
#endif
	QString json = "{\"BaseRequest\":{\"Uin\":" + uin + ",\"Sid\":\"" + sid
		+ "\",\"Skey\":\"" + skey + "\",\"DeviceID\":\"" + deviceId
		+ "\"},\"Msg\":{\"FromUserName\":\"" + fromUserName
		+ "\",\"ToUserName\":\"" + toUserName
		+ "\",\"Type\":1,\"Content\":\"" + content.replace("\"", "\\\"")
		+ "\",\"ClientMsgId\":" + ts
		+ ",\"LocalID\":" + ts + "}}";
#if 0//QWX_DEBUG
	qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << json;
#endif
	Http_Post::post(url, json, true);
}

void Send_Msg::postV2(QString uin, QString sid, QString skey, QString deviceId,
						QString fromUserName, QString toUserName, QString content)
{
	m_post(WX_V2_SERVER_HOST,
		   uin,
		   sid,
		   skey,
		   deviceId,
		   fromUserName,
		   toUserName,
		   content);
}

void Send_Msg::m_sync(QString host, QString uin, QString sid, QString skey, QStringList syncKey)
{
	QString ts = QString::number(time(NULL));
	QString url = host + WX_CGI_PATH + "webwxsync?sid=" + sid +
		"&skey=" + skey + "&r=" + ts;
#if 0//QWX_DEBUG
	qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
#endif
	QString json = "{\"BaseRequest\":{\"Uin\":" + uin + ",\"Sid\":\"" + sid +
		"\"},\"SyncKey\":{\"Count\":" + QString::number(syncKey.size()) +
		",\"List\":[";
	for (int i = 0; i < syncKey.size(); i++) {
		if (i != 0)
			json += ",";
		QStringList result = syncKey[i].split("|");
		json += "{\"Key\":" + result[0] + ",\"Val\":" + result[1] + "}";
	}
	json += "]},\"rr\":" + ts + "}";
#if QWX_DEBUG
	qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << json;
#endif
	Http_Post::post(url, json, true);
}

void Send_Msg::syncV2(QString uin, QString sid, QString skey, QStringList syncKey)
{
	m_sync(WX_V2_SERVER_HOST, uin, sid, skey, syncKey);
}

void Send_Msg::finished(QNetworkReply* reply)
{
	QString replyStr = QString(reply->readAll());

#if 0//QWX_DEBUG
	qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
    //qDebug() << "DEBUG:" << replyStr;
	QFile file("sendmsg.json");
	if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
		QTextStream out(&file);
		out << replyStr;
		file.close();
	}
#endif
}
